import CryptoJS from 'crypto-js'
export function isWeChatBrowser() {
	var userAgent = window.navigator.userAgent.toLowerCase()
	return userAgent.match(/micromessenger/i) !== null
}

function randomString() {
	let result = ''
	const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
	const charactersLength = characters.length
	for (let i = 0; i < 32; i++) {
		result += characters.charAt(Math.floor(Math.random() * charactersLength))
	}
	return result
}

export function signfn(url, appKey = 'iosb83d14cd79b4098f3da9b4933ccdd', key =
'ios11ee86c3fdc3d14cd9a48f3dgjkad') {
	const authTs = new Date().getTime()
	const authOnce = randomString()
	const authSign = CryptoJS.MD5(`act=${url}&app_key=${appKey}&nonce=${authOnce}&ts=${authTs}&key=${key}`)
		.toString()
	return {
		'auth-ts': authTs,
		'auth-nonce': authOnce,
		'auth-sign': authSign,
		'auth-key': appKey,
		// 'auth-type-id': 1,
	}
}
// 判断是否为空
export function isNotEmpty(value) {
	return value !== null && value !== undefined && value !== '';
}
// 过滤掉空属性
export function filterEmptyProperties(obj) {
	const filteredObj = {};
	Object.keys(obj).forEach(key => {
		if (isNotEmpty(obj[key])) {
			filteredObj[key] = obj[key];
		}
	});
	return filteredObj;
}

export function convertObjectToQueryString(obj) {
	return Object.entries(obj)
		.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
		.join('&');
}